package com.xujiang.myblog.tools;

/**
 * Created by xujiang on 2017/4/14.
 */

import android.os.Build;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;

/**
 * SD卡相关的辅助类
 *
 *@author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-11-03
 * @author <a href="http://www.cnblogs.com/smiler/p/3854761.html" target="_blank">幕三少</a>
 */

public class SDCardUtils {
    private static final String TAG = SDCardUtils.class.getSimpleName();

    /**
     * 判断SDCard是否可用
     *
     * @return
     */
    public static boolean isSDCardEnable() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    /**
     * 获得sd卡剩余容量，即可用大小
     * @return byte
     */
    public static long getSDAvailableSize() {
        StatFs statFs = new StatFs(getSDCardPath().getAbsolutePath());
        long blockSize;
        long availableBlocks;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = statFs.getBlockSizeLong();
            availableBlocks = statFs.getAvailableBlocksLong();
        } else {
            blockSize = statFs.getBlockSize();
            availableBlocks = statFs.getAvailableBlocks();
        }
        return blockSize * availableBlocks;
    }

    /**
     * 获取SD卡路径
     *
     * @return
     */
    public static File getSDCardPath() {
        return Environment.getExternalStorageDirectory();
    }




}
